package com.chixing.controller;

import com.chixing.service.ElasticSearchService;
import com.chixing.service.SearchService;
import com.chixing.util.ServerResult;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.io.IOException;

@Controller
public class SearchController {

    @Autowired
    private SearchService searchService;

    @Autowired
    private ElasticSearchService elasticSearchService;

    @GetMapping("car/{keyword}/page/{pageNum}")
    public ModelAndView carSearch(@PathVariable("keyword") String keyword,
                                  @PathVariable("pageNum") Integer pageNum) {
        System.out.println("========keyword=======:" + keyword);
        System.out.println("========pagenum=======:" + pageNum);

        ServerResult serverResult = searchService.getByKeyword(keyword, pageNum);

        System.out.println("========查询数据信息=======\n" + serverResult.getData());

        ModelAndView mav = new ModelAndView();

        if (serverResult.getCode() == 200) {
            mav.addObject("keyword", keyword);
            mav.addObject("pageInfo", serverResult.getData());
        } else {
            mav.addObject("dataInfo", serverResult.getData());
        }
        mav.setViewName("car_rental/car-search");

        return mav;
    }


    //默认无店铺id进入
    @GetMapping("car/getAll/page/{pageNum}")
    public ModelAndView getAll(@PathVariable("pageNum") Integer pageNum) {

        System.out.println("========pagenum=======:" + pageNum);
        ServerResult serverResult = searchService.getAll(null, pageNum);
        ModelAndView mav = new ModelAndView();

        if (serverResult.getCode() == 200) {
            mav.addObject("pageInfoAll", serverResult.getData());
        }

        mav.setViewName("car_rental/car-search");
        return mav;

    }

    //有店铺id进入
    @GetMapping("shop/{shopId}/page/{pageNum}")
    public ModelAndView getAll(@PathVariable("shopId") Integer shopId, @PathVariable("pageNum") Integer pageNum, @RequestParam( name="carId",required = false) Integer carId) {
        System.out.println("========shopId=======:" + shopId);
        System.out.println("========pagenum=======:" + pageNum);
        System.out.println("========carId=======:" + carId);
        ServerResult serverResult = null;
        if(carId==null){
             serverResult = searchService.getAll(shopId, pageNum);
        }else {
             serverResult = searchService.getByCarAndShop(shopId,pageNum,carId);
        }
        ModelAndView mav = new ModelAndView();

        if (serverResult.getCode() == 200) {
            mav.addObject("pageInfoAll", serverResult.getData());
        }

        mav.setViewName("car_rental/car-search");
        return mav;

    }

    // 多条件动态查询
    // 多条件动态查询
    @GetMapping("car/ContainAndDate")
    @ResponseBody
    public ServerResult getCarByContain(@Param("carType") String carType,
                                        @Param("carBrand") String carBrand,
                                        @Param("minCarDailyPrice") Float minCarDailyPrice,
                                        @Param("maxCarDailyPrice") Float maxCarDailyPrice,
                                        @Param("carShiftType") String carShiftType,
                                        @Param("carSeatNumber") Integer carSeatNumber,
                                        @Param("orderBy") String orderBy,
                                        @Param("pageNum") Integer pageNum) {

        System.out.println("carType：" + carType);
        System.out.println("carBrand：" + carBrand);
        System.out.println("minCarDailyPrice：" + minCarDailyPrice);
        System.out.println("maxCarDailyPrice：" + maxCarDailyPrice);
        System.out.println("carShiftType：" + carShiftType);
        System.out.println("carSeatNumber：" + carSeatNumber);
        System.out.println("orderBy：" + orderBy);
        System.out.println("pageNum：" + pageNum);

        ServerResult result = searchService.getByMoreKeyword(carType, carBrand, minCarDailyPrice, maxCarDailyPrice, carShiftType, carSeatNumber, pageNum);
        return result;
//        return searchService.getCarByContain(carType,carBrand,minCarDailyPrice,maxCarDailyPrice,carShiftType,carSeatNumber,orderBy,pageNum);
    }

    //店铺列表
    @GetMapping("store/{storeCity}")
    @ResponseBody
    public ServerResult getShopByCity(@PathVariable("storeCity") String shopCity) {


        ServerResult result = searchService.getShopByCity(shopCity);
        return result;
//        return searchService.getCarByContain(carType,carBrand,minCarDailyPrice,maxCarDailyPrice,carShiftType,carSeatNumber,orderBy,pageNum);
    }


    @GetMapping("/saveToEs")
    @ResponseBody
    public Boolean parse() throws IOException {
        return elasticSearchService.parseContent();
    }

    @GetMapping("/search/{keyWord}/{pageNo}")
    public ModelAndView search(@PathVariable("keyWord") String keyWord,
                               @PathVariable("pageNo") int pageNo) throws IOException {

        if(pageNo == 0){
            pageNo = 1;
        }

//        return gameService.searchPage(keyWord,pageNo,pageSize);
        ModelAndView mav = new ModelAndView();

        ServerResult serverResult = elasticSearchService.searchPage(keyWord,pageNo);
        if(serverResult.getCode() == 200){
            mav.addObject("keyWord",keyWord);
            mav.addObject("pageInfo",serverResult.getData());
            System.out.println(serverResult.getData());
        }else{
            mav.addObject("dataInfo",serverResult.getData());
        }
        mav.setViewName("car_rental/car-search");

//        System.out.println(serverResult.getData());
        return mav;

    }


}
